package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.PigDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TransferDAO;

import cn.edu.scau.cmi.hombio.domain.Pig;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.Transfer;

import cn.edu.scau.cmi.hombio.service.TransferService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Transfer entities
 * 
 */

@Controller("TransferRestController")

public class TransferRestController {

	/**
	 * DAO injected by Spring that manages Pig entities
	 * 
	 */
	@Autowired
	private PigDAO pigDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages Transfer entities
	 * 
	 */
	@Autowired
	private TransferDAO transferDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Transfer entities
	 * 
	 */
	@Autowired
	private TransferService transferService;

	/**
	 * Create a new Pig entity
	 * 
	 */
	@RequestMapping(value = "/Transfer/{transfer_id}/pig", method = RequestMethod.POST)
	@ResponseBody
	public Pig newTransferPig(@PathVariable Integer transfer_id, @RequestBody Pig pig) {
		transferService.saveTransferPig(transfer_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Save an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/task", method = RequestMethod.PUT)
	@ResponseBody
	public Task saveTransferTask(@PathVariable Integer transfer_id, @RequestBody Task task) {
		transferService.saveTransferTask(transfer_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Save an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pigsty", method = RequestMethod.PUT)
	@ResponseBody
	public Pigsty saveTransferPigsty(@PathVariable Integer transfer_id, @RequestBody Pigsty pigsty) {
		transferService.saveTransferPigsty(transfer_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* View an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/task/{task_id}", method = RequestMethod.GET)
	@ResponseBody
	public Task loadTransferTask(@PathVariable Integer transfer_id, @PathVariable Integer related_task_id) {
		Task task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		return task;
	}

	/**
	* Create a new Task entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/task", method = RequestMethod.POST)
	@ResponseBody
	public Task newTransferTask(@PathVariable Integer transfer_id, @RequestBody Task task) {
		transferService.saveTransferTask(transfer_id, task);
		return taskDAO.findTaskByPrimaryKey(task.getId());
	}

	/**
	* Get Pigsty entity by Transfer
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pigsty", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty getTransferPigsty(@PathVariable Integer transfer_id) {
		return transferDAO.findTransferByPrimaryKey(transfer_id).getPigsty();
	}

	/**
	* Delete an existing Task entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/task/{task_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTransferTask(@PathVariable Integer transfer_id, @PathVariable Integer related_task_id) {
		transferService.deleteTransferTask(transfer_id, related_task_id);
	}

	/**
	* View an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pigsty/{pigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty loadTransferPigsty(@PathVariable Integer transfer_id, @PathVariable Integer related_pigsty_id) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		return pigsty;
	}

	/**
	* Create a new Transfer entity
	* 
	*/
	@RequestMapping(value = "/Transfer", method = RequestMethod.POST)
	@ResponseBody
	public Transfer newTransfer(@RequestBody Transfer transfer) {
		transferService.saveTransfer(transfer);
		return transferDAO.findTransferByPrimaryKey(transfer.getId());
	}

	/**
	* Delete an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pigsty/{pigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTransferPigsty(@PathVariable Integer transfer_id, @PathVariable Integer related_pigsty_id) {
		transferService.deleteTransferPigsty(transfer_id, related_pigsty_id);
	}

	/**
	* Get Task entity by Transfer
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/task", method = RequestMethod.GET)
	@ResponseBody
	public Task getTransferTask(@PathVariable Integer transfer_id) {
		return transferDAO.findTransferByPrimaryKey(transfer_id).getTask();
	}

	/**
	* View an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pig/{pig_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pig loadTransferPig(@PathVariable Integer transfer_id, @PathVariable Integer related_pig_id) {
		Pig pig = pigDAO.findPigByPrimaryKey(related_pig_id, -1, -1);

		return pig;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Select an existing Transfer entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}", method = RequestMethod.GET)
	@ResponseBody
	public Transfer loadTransfer(@PathVariable Integer transfer_id) {
		return transferDAO.findTransferByPrimaryKey(transfer_id);
	}

	/**
	* Save an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pig", method = RequestMethod.PUT)
	@ResponseBody
	public Pig saveTransferPig(@PathVariable Integer transfer_id, @RequestBody Pig pig) {
		transferService.saveTransferPig(transfer_id, pig);
		return pigDAO.findPigByPrimaryKey(pig.getId());
	}

	/**
	* Delete an existing Pig entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pig/{pig_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTransferPig(@PathVariable Integer transfer_id, @PathVariable Integer related_pig_id) {
		transferService.deleteTransferPig(transfer_id, related_pig_id);
	}

	/**
	* Get Pig entity by Transfer
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pig", method = RequestMethod.GET)
	@ResponseBody
	public Pig getTransferPig(@PathVariable Integer transfer_id) {
		return transferDAO.findTransferByPrimaryKey(transfer_id).getPig();
	}

	/**
	* Delete an existing Transfer entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteTransfer(@PathVariable Integer transfer_id) {
		Transfer transfer = transferDAO.findTransferByPrimaryKey(transfer_id);
		transferService.deleteTransfer(transfer);
	}

	/**
	* Save an existing Transfer entity
	* 
	*/
	@RequestMapping(value = "/Transfer", method = RequestMethod.PUT)
	@ResponseBody
	public Transfer saveTransfer(@RequestBody Transfer transfer) {
		transferService.saveTransfer(transfer);
		return transferDAO.findTransferByPrimaryKey(transfer.getId());
	}

	/**
	* Create a new Pigsty entity
	* 
	*/
	@RequestMapping(value = "/Transfer/{transfer_id}/pigsty", method = RequestMethod.POST)
	@ResponseBody
	public Pigsty newTransferPigsty(@PathVariable Integer transfer_id, @RequestBody Pigsty pigsty) {
		transferService.saveTransferPigsty(transfer_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Show all Transfer entities
	* 
	*/
	@RequestMapping(value = "/Transfer", method = RequestMethod.GET)
	@ResponseBody
	public List<Transfer> listTransfers() {
		return new java.util.ArrayList<Transfer>(transferService.loadTransfers());
	}
}